package LeetCode.interview;

import util.LogUtils;

/*
 * 
 原题

　　Given an array and a value, remove all instances of that value in place and return the new length. 
　　The order of elements can be changed. It doesn’t matter what you leave beyond the new length. 

题目大意

　　给定一个数组和一个值，删除数组中与这个值相等的元素，并且返回与这个数组的新的长度。 

解题思路	
	数组删除元素：删除元素后，后面的元素全部向前移动一格
	
 * @Date 2017-09-12 18：34
 */
public class _027_Remove_Element {
    public int removeElement(int[] nums, int val) {
    	if (nums==null || nums.length==0)	return 0;
    	int N = nums.length;
    	for (int i = 0; i < N; ) {
    		if (nums[i] == val) {
    			//删除该元素
    			for (int j = i; j < N-1; j ++) {
    				nums[j]= nums[j+1];
    			}
    			N--;
    			//遍历：看下结果
    			traverse(nums, N);
    		} else {
    			i ++;
    		}
    	}
        return N;
    }
    private void traverse(int[] nums, int end) {
    	for (int i = 0; i < end; i ++) {
    		LogUtils.print(nums[i]);
    	}
    	LogUtils.br();
    }
	public static void main(String[] args) {
		LogUtils.println("结果", new _027_Remove_Element().removeElement(new int[] {1, 2, 2, 4, 4, 5}, 4));
		
	}
}
